<template>
  <a-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="指标部门" :labelCol="labelCol" :wrapperCol="wrapperCol">
          
          <j-dict-select-tag  v-decorator="['indexDepart',  validatorRules.indexDepart]" :triggerChange="true" placeholder="请输入用户性别"
            dictCode="sys_depart,depart_name,org_code"/>
        </a-form-item>      
        
        <a-form-item label="公式日期标识" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input type="list" v-decorator="['whichData', validatorRules.whichData]" :trigger-change="true" dictCode="" placeholder="请填写公式日期标识"/>
        </a-form-item>
        <a-form-item label="指标值编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input type="list" v-decorator="['indexValueCode', validatorRules.indexValueCode]" :trigger-change="true" dictCode="" placeholder="请填写公式日期标识"/>
        </a-form-item>
        <a-form-item label="变量计算公式" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-textarea v-decorator="['indexValueRule', validatorRules.indexValueRule]" rows="4" placeholder="请输入变量计算公式"/>
        </a-form-item>
        <a-form-item label="中文计算公式" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-textarea v-decorator="['indexValueRuleCn', validatorRules.indexValueRuleCn]" rows="4" placeholder="请输入中文计算公式"/>
        </a-form-item>
        <a-form-item label="指标值规则说明" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-textarea v-decorator="['indexValueRuleComments', validatorRules.indexValueRuleComments]" rows="4" placeholder="请输入指标值规则说明"/>
        </a-form-item>

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import {getAllOrgs,getIndexCodes,getIndexValueCodes} from '@/api/api.js'
  /* 封装通用 api 服务 */
  import { apiService } from '@/api/api.service.js'
  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JDictSelectTag from "@/components/dict/JDictSelectTag"

  export default {
    name: "TkIndexValueRuleModal",
    components: { 
      JDictSelectTag,
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title:"操作",
        width:800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          indexDepart: {rules: [
            {required: true, message: '请选择部门!'},
          ]},
          whichData: {rules: [
            {required: true, message: '请输入公式日期标识!'},
          ]},
          indexValueCode: {rules: [
            {required: true, message: '请输入指标值编码!'},
          ]},
          indexValueRule: {rules: [
            {required: true, message: '请输入变量计算公式!'},
          ]},
          indexValueRuleCn: {rules: [
            {required: true, message: '请输入中文计算公式!'},
          ]},
          indexValueRuleComments: {rules: [
          ]},
        },
        url: {
          add: "/tkindexvaluerule/tkIndexValueRule/add",
          edit: "/tkindexvaluerule/tkIndexValueRule/edit",
        },
        indexDeparts:[{
          value:'',
          text:'全部'
        }],
        indexCodes:[{
          code:'',
          name:'全部'
        }],
        indexValueCodes:[{
          code:'',
          name:'全部'
        }],
      }
    },
    created () {
    },
    methods: {
      changeIndexDepart(indexDepart){
        let indexGroup;
        if(indexDepart.substr(0,9) == 'A01A01A02' || indexDepart.substr(0,2) == 'DG'){
          indexGroup = 'G';
        }else if(indexDepart.substr(0,9) == 'A01A01A01'){
          indexGroup = 'C';
        }
        let param = {
            indexGroup: indexGroup
        }
        apiService(getIndexCodes(param)).then((res) => {
            let result = res.result || {};
            this.indexCodes=result;
            this.indexCodes.unshift({
              code:'',
              name:'全部'
            });
            this.model.indexCode = '';
        }).catch((error) => {
            this.$message.error(error.message);
        });  
      },
      changeIndexCodes(indexCode){
          let indexGroup;
          if(this.queryParam.indexDepart.substr(0,9) == 'A01A01A02' || this.queryParam.indexDepart.substr(0,2) == 'DG'){
            indexGroup = '1';
          }else if(this.queryParam.indexDepart.substr(0,9) == 'A01A01A01'){
            indexGroup = '2';
          }
          let param = {
            indexCode: indexCode,
            indexGroup:indexGroup
          }
          apiService(getIndexValueCodes(param)).then((res) => {
            let result = res.result || {};
            this.indexValueCodes = result;
            this.indexValueCodes.unshift({
              code:'',
              name:'全部'
            });
            this.queryParam.indexValueCode = '';
          }).catch((error) => {
            this.$message.error(error.message);
          });
      },
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'indexDepart','whichData','indexValueCode','indexValueRule','indexValueRuleCn','indexValueRuleComments'))
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }
         
        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'indexDepart','whichData','indexValueCode','indexValueRule','indexValueRuleCn','indexValueRuleComments'))
      },

      
    }
  }
</script>